package com.example.w_library.utils;

import android.view.View;
import android.view.ViewGroup;

import com.example.w_library.log.WLog;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.logging.Logger;

public class ViewUtil {

    /**
     * 获取 group 内指定类型的子 view
     * @param group ViewGroup
     * @param clazz 如:RecyclerView.class
     * @param <T>
     * @return
     */
    public static <T> T findTypeView(ViewGroup group,Class<T> clazz){
        if (group == null){
            return null;
        }
        //双端队列,快速添加删除第一个或者最后一个元素
        Deque<View> deque = new ArrayDeque<>();
        deque.add(group);

        while (!deque.isEmpty()){

            View node = deque.removeFirst();

            if (clazz.isInstance(node)){

                return clazz.cast(node);

            }else if (node instanceof ViewGroup){

                ViewGroup container = (ViewGroup) node;
                //加入下一级的子view
                for (int i = 0, count = container.getChildCount(); i < count; i++) {
                    deque.add(container.getChildAt(i));
                }
            }

        }

        return null;
    }
}
